package com.be.beadmin.system.controller.ProductQualityController;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.product.dto.input.GuideBookFileInfoDto;
import com.be.beadmin.product.dto.output.GuideBookFileInfoEx;
import com.be.beadmin.product.service.GuideBookFileInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author cms
* @date 2023-09-19
**/

@Api(tags = {"指导书管理-关联批次柜体"})
@RestController
public class GuideBookFileInfoPqController {

    @Autowired
    private GuideBookFileInfoService guideBookFileInfoService;


    @ApiOperation("分页查询文件信息")
    @PostMapping(value = "/product/getGuideBookFileInfoBybatchProductId")
    public ResultModel getGuideBookFileInfoByPage(@RequestParam String batchProductId) {
        return ResultModel.success(guideBookFileInfoService.getGuideBookFileInfoBybatchProductId(batchProductId));
}

    @PostMapping("/product/addGuideBookFileInfo2")
    public ResultModel addGuideBookFileInfo2(@RequestBody GuideBookFileInfoDto guideBookFileInfoDto) {
        guideBookFileInfoService.addGuideBookFileInfo(guideBookFileInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }


    @ApiOperation("删除文件信息")
    @PostMapping("/product/delGuideBookFileInfo2")
    public ResultModel delGuideBookFileInfo2(@RequestBody GuideBookFileInfoDto guideBookFileInfoDto) {
        guideBookFileInfoService.delGuideBookFileInfo(guideBookFileInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("更新文件信息")
    @PostMapping("/product/updateGuideBookFileInfo2")
    public ResultModel updateGuideBookFileInfo2(@RequestBody GuideBookFileInfoDto guideBookFileInfoDto) {
        guideBookFileInfoService.updateGuideBookFileInfo(guideBookFileInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("导出所有文件信息")
    @PostMapping("/product/exportGuideBookFileInfo2")
    public void exportGuideBookFileInfo2(HttpServletResponse response, @RequestBody PageEntity pageEntity) throws IOException {
        Page<GuideBookFileInfoEx> guideBookFileInfoPage = guideBookFileInfoService.getGuideBookFileInfoByPage(pageEntity);
        List<GuideBookFileInfoEx> guideBookFileInfoExes = guideBookFileInfoPage.getContent();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("GuideBookFileInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        EasyExcel.write(response.getOutputStream(), GuideBookFileInfoEx.class).excelType(ExcelTypeEnum.XLS).sheet("sheet1").doWrite(guideBookFileInfoExes);
    }

    @ApiOperation("批量上传指导书")
    @PostMapping("/product/uploadGuideBookFileInfo2")
    public ResultModel uploadInspectionFileInfo2(@ApiParam(value = "批次柜体id", required = true) @RequestParam String batchProductId,
                                                 @ApiParam(value = "上传人id", required = true) @RequestParam String uploaderId,
                                                 @ApiParam(value = "上传人姓名", required = true) @RequestParam String uploaderName,
                                                 @ApiParam(value = "上传时间", required = true) @RequestParam String uploadTime,
                                                 @ApiParam(value = "文件说明") @RequestParam (required = false) String instruction,
                                                @RequestParam(value = "file") MultipartFile[] multipartFiles) {

        GuideBookFileInfoDto guideBookFileInfoDto = new GuideBookFileInfoDto();
        guideBookFileInfoDto.setBatchProductId(batchProductId);
        guideBookFileInfoDto.setUploaderId(uploaderId);
        guideBookFileInfoDto.setUploaderName(uploaderName);
        guideBookFileInfoDto.setUploadTime(DateUtil.parse(uploadTime));
        guideBookFileInfoDto.setInstruction(instruction);
        guideBookFileInfoService.uploadGuideBookFileInfo(guideBookFileInfoDto, multipartFiles);
        return ResultModel.success(ResultModel.ADD);
    }

    @ApiOperation("获得pdf、图片的预览url")
    @PostMapping("/product/getGuideBookFileInfoPreviewUrl2")
    public ResultModel getGuideBookFileInfoPreviewUrl2(@RequestBody GuideBookFileInfoDto guideBookFileInfoDto)  {
        String previewUrl =  guideBookFileInfoService.getGuideBookFileInfoPreviewUrl(guideBookFileInfoDto);
        return ResultModel.success(previewUrl);
    }

    @ApiOperation("其他文件类型附件下载")
    @PostMapping("/product/downloadGuideBookFile2")
    public void downloadGuideBookFile2(@RequestBody GuideBookFileInfoDto guideBookFileInfoDto,
                                        HttpServletResponse response) {

        guideBookFileInfoService.downloadGuideBookFile(guideBookFileInfoDto, response);
    }

}